/**
 * IsvRequestParamStringXmlAssemble.java
 * author: yujiakui
 * 2017年8月29日
 * 下午4:07:12
 */
package com.ctim.service.component.assemble.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.ctim.model.IsvRequestParamInfo;
import com.ctim.service.component.assemble.IsvRequestParamAssemble;
import com.ctim.service.enums.IsvRequestParamAssembleTypeEnum;
import com.google.common.collect.Lists;

/**
 * @author yujiakui
 *
 *         下午4:07:12 渠道请求参数字符串返回结果xml形式的组装器
 */
@Component("isvRequestParamStringXmlAssemble")
public class IsvRequestParamStringXmlAssemble implements IsvRequestParamAssemble {

	/*
	 *
	 * @see
	 * com.ctim.service.component.assemble.IsvRequestParamAssemble#assemble(com.
	 * ctim.model.IsvRequestParamInfo)
	 */
	@Override
	public String assemble(IsvRequestParamInfo requestInfo) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
		stringBuilder.append("<conditions>");
		stringBuilder.append("<condition queryType=\"");
		stringBuilder.append(requestInfo.getReqType());
		stringBuilder.append("\">");
		// 设置item
		stringBuilder.append(assembleItems(requestInfo.getReqCndMap()));
		stringBuilder.append("</condition>");
		stringBuilder.append("</conditions>");
		return stringBuilder.toString();
	}

	/**
	 * 组装items
	 *
	 * @param itemInfoMap
	 * @return
	 */
	private String assembleItems(Map<String, String> itemInfoMap) {
		StringBuilder stringBuilder = new StringBuilder();

		for (Map.Entry<String, String> entryEle : itemInfoMap.entrySet()) {
			stringBuilder.append("<item>");
			stringBuilder.append("<name>");
			stringBuilder.append(entryEle.getKey());
			stringBuilder.append("</name>");
			stringBuilder.append("<value>");
			stringBuilder.append(entryEle.getValue());
			stringBuilder.append("</value>");
			stringBuilder.append("</item>");
		}
		return stringBuilder.toString();
	}

	/*
	 *
	 * @see com.ctim.service.component.assemble.IsvRequestParamAssemble#
	 * getAssembleType()
	 */
	@Override
	public List<IsvRequestParamAssembleTypeEnum> getAssembleType() {
		return Lists.newArrayList(IsvRequestParamAssembleTypeEnum.XML);
	}

}
